package com.chlp.core.framework.utils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询帮助类
 *
 * @author ChenLiPeng
 */
public class AssistInsert {

	/**
	 * 表名
	 */
	private String table;
	/**
	 * 表主键的字段
	 */
	private String key;
	/**
	 * 表自增主键的返回值
	 */
	private Integer primaryKey;
	/**
	 * 表的字段
	 */
	private List<FieldClass> fields = new ArrayList<>();

	public void handleField(Object object, String type) {
		if (object == null) {
			return;
		}
		Class<?> aClass = object.getClass();
		Table table = aClass.getAnnotation(Table.class);
		this.table = table.name();

		// 获取对象的所有的属性集合
		Field[] fields = aClass.getDeclaredFields();
		for (Field field : fields) {
			//获取属性名称
			String fieldName = field.getName();
			// 属性对应列表的字段
			String fieldTable = null;
			//获取属性值
			Object fieldValue = null;
			try {
				// 打开私有访问
				field.setAccessible(true);
				fieldValue = field.get(object);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

			if (field.isAnnotationPresent(Column.class)) {
				fieldTable = field.getDeclaredAnnotation(Column.class).name();
			}
			if (field.isAnnotationPresent(Id.class)) {
				this.key = field.getDeclaredAnnotation(Column.class).name();
				this.primaryKey = (Integer) fieldValue;
				// 主键必须自增
				continue;
			}

			FieldClass fieldClass = new FieldClass();
			fieldClass.setFieldName(fieldName);

			if ("insert".equals(type)) {
				// 给实体通用属性赋值
				switch (fieldName) {
					case "createUser":
					case "lastUpdateUser":
						fieldValue = "user";
						break;
					case "createTime":
					case "lastUpdateTime":
						fieldValue = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
						break;
					case "markStatus":
						fieldValue = "0";
						break;
					case "companyCode":
						fieldValue = "code";
						break;
					default:
				}
			}
			if ("update".equals(type)) {
				// 给实体通用属性赋值
				if ("lastUpdateUser".equals(fieldName)) {
					fieldValue = "user";
				} else if ("lastUpdateTime".equals(fieldName)) {
					fieldValue = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
				}
			}

			fieldClass.setFieldValue(fieldValue);
			fieldClass.setFieldTable(fieldTable);
			this.fields.add(fieldClass);
		}
	}


	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public List<FieldClass> getFields() {
		return fields;
	}

	public void setFields(List<FieldClass> fields) {
		this.fields = fields;
	}

	public Integer getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(Integer primaryKey) {
		this.primaryKey = primaryKey;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public static class FieldClass {
		private String fieldName;
		private Object fieldValue;
		private String fieldTable;

		public String getFieldName() {
			return fieldName;
		}

		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
		}

		public Object getFieldValue() {
			return fieldValue;
		}

		public void setFieldValue(Object fieldValue) {
			this.fieldValue = fieldValue;
		}

		public String getFieldTable() {
			return fieldTable;
		}

		public void setFieldTable(String fieldTable) {
			this.fieldTable = fieldTable;
		}
	}

}

